﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
using XingLucifer.IBase.Enums;

namespace Kinlo.MESDocking
{
    /// <summary>
    /// 工单获取接口
    /// </summary>
    public class GetByProductCodeInterface : MESInteractionBase
    {
        /// <summary>
        /// 工单获取接口
        /// </summary>
        /// <param name="log"></param>
        /// <param name="dialog"></param>
        public GetByProductCodeInterface(Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog)
            : base(log, dialog, "工单获取接口")
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="timeout">超时时间</param>
        /// <param name="reconnect_count">失败重试次数</param>
        /// <param name="dateTime">当前时间</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public async override Task<ResultTypes> MESInteractionAsync(string url, int timeout, int reconnect_count, DateTime dateTime, Dictionary<string, object> parameters)
        {
            _stopwatch.Restart();
            string json = JsonSerializer.Serialize(parameters, HttpHelper.Options);
            _stopwatch.Stop();
            var binding = new BasicHttpBinding();
            var endpoint = new EndpointAddress(url); // url地址
            var serv = new WebServiceWorkOrder.JGWebServiceSoapClient(binding, endpoint);

            var mesResult = await serv.List_WorkOrderAsync(parameters["process_id"].ToString(), parameters["equipment_id"].ToString()); // xml格式的字符串参数  

            //string mesResult = "{\"success\":true,\"returnMsg\":\"\",\"statusCode\":\"200\",\"resultData\":[{\"Bill_No\":\"SF23010301001\",\"ProductionOrderNO\":\"SF230103010\",\"PC_No\":null,\"Material_Code\":\"DC20201 - 1816 - 002\",\"Material_ID\":\"1002001110014625\",\"Material_Spec\":\"18650H - 1600 - 3.2V / 高温型\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 01 - 07\",\"PlanedEndDate\":\"2023 - 01 - 07\"},{\"Bill_No\":\"SF23011001401\",\"ProductionOrderNO\":\"SF230110014\",\"PC_No\":null,\"Material_Code\":\"DC20201 - 1811 - 002\",\"Material_ID\":\"1001911300110585\",\"Material_Spec\":\"18650 - 1100 - 3.2V\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 01 - 13\",\"PlanedEndDate\":\"2023 - 01 - 13\"},{\"Bill_No\":\"SF23022101201\",\"ProductionOrderNO\":\"SF230221012\",\"PC_No\":null,\"Material_Code\":\"DC20201 - 1816 - 002\",\"Material_ID\":\"1002001110014625\",\"Material_Spec\":\"18650H - 1600 - 3.2V / 高温型\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 03 - 04\",\"PlanedEndDate\":\"2023 - 03 - 04\"}]}";

            _log?.Invoke("", _name, mesResult, json, mesResult != null ? "请求成功" : "请求异常",
                _stopwatch.ElapsedMilliseconds, dateTime, parameters["process_id"]?.ToString() ?? "", parameters["equipment_id"]?.ToString() ?? "", this.GetType().Name, url);

            if (mesResult != null)
            {
                var result_json = JsonSerializer.Deserialize<Dictionary<string, object>>(mesResult.ToString());
                var ttt=result_json["success"].ToString().ToUpper();
                if (result_json["success"].ToString().ToUpper() == "TRUE" && result_json["statusCode"].ToString() == "200")
                {
                    parameters.Clear();
                    if(!string.IsNullOrWhiteSpace(result_json["resultData"].ToString()))
                    {
                        parameters.Add("result", JsonSerializer.Deserialize<List<Dictionary<string, object>>>(result_json["resultData"].ToString()));
                    }
                    else
                    {
                        parameters.Add("result",new List<Dictionary<string, object>>());
                    }
                  
                    return ResultTypes.合格;
                }
                else
                {
                    _dialog?.Invoke(_name, result_json["code"].ToString(), result_json["message"].ToString());
                }
            }
            else
            {
                _dialog?.Invoke(_name, "ERROR", mesResult);
            }
            return ResultTypes.MES_NG;
        }



    }

 
}
